﻿using System.Collections.Generic;
using System.Text;
using System.Windows;

namespace Bing.Translate
{
    /// <summary>
    /// Interaction logic for TranslateWindow.xaml
    /// </summary>
    public partial class TranslateWindow
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="TranslateWindow"/> class.
        /// </summary>
        public TranslateWindow()
        {
            InitializeComponent();
            cboFrom.DisplayMemberPath = cboTo.DisplayMemberPath = "Value";
            cboFrom.SelectedValuePath = cboTo.SelectedValuePath = "Key";
            cboFrom.ItemsSource = GetLanguages();
            cboTo.ItemsSource = GetLanguages();
            cboFrom.SelectedValue = "En";
            cboTo.SelectedValue = "Es";
        }

        private void btnTranslate_Click(object sender, RoutedEventArgs e)
        {
            var appId = txtAppId.Text.Trim();
            var query = txtToTranslate.Text.Trim();
            txtResults.Text = "";
            expander1.IsExpanded = false;
    
            if (string.IsNullOrEmpty(query)) return;

            SearchRequest searchRequest = new SearchRequest() { AppId = appId, Query = query, Market = "en-US" };

            TranslationRequest translationRequest = new TranslationRequest();
            translationRequest.SourceLanguage = cboFrom.SelectedValue.ToString();
            translationRequest.TargetLanguage = cboTo.SelectedValue.ToString();

            TranslationResponse response = API.Translation(searchRequest, translationRequest);

            if (response.Errors.Count > 0 )
            {
                // Display Errors
                MessageBox.Show(bingErrorMessage(response.Errors), "Bing.API Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (response.TranslationResults.Count > 0)
            {
                txtResults.Text = response.TranslationResults[0].TranslatedTerm;
                expander1.IsExpanded = true;
            }
        }

        private static string bingErrorMessage(IEnumerable<Error> errors)
        {
            StringBuilder text = new StringBuilder();
            int counter = 1;
            foreach (Error error in errors)
            {
                text.AppendFormat("Error #{0}.\r\n", counter);
                text.AppendFormat("\tMessage: {0}\r\n", error.Message);
                text.AppendFormat("\tCode: {0}\r\n", error.Code);
                text.AppendFormat("\tHelpUrl: {0}\r\n", error.HelpUrl);
                text.AppendFormat("\tParameter: {0}\r\n", error.Parameter);
                text.AppendFormat("\tValue: {0}\r\n", error.Value);
                counter++;
            }

            return text.ToString();
        }

        /// <summary>
        /// Gets the languages.
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetLanguages()
        {
            Dictionary<string, string> languages = new Dictionary<string, string>();
            languages.Add("Ar", "Arabic");
            languages.Add("zh-CHS", "Simplified Chinese");
            languages.Add("zh-CHT", "Traditional Chinese");
            languages.Add("Nl", "Dutch");
            languages.Add("En", "English");
            languages.Add("Fr", "French");
            languages.Add("De", "German");
            languages.Add("It", "Italian");
            languages.Add("Ja", "Japanese");
            languages.Add("Ko", "Korean");
            languages.Add("Pl", "Polish");
            languages.Add("Pt", "Portuguese");
            languages.Add("Ru", "Russian");
            languages.Add("Es", "Spanish");
            return languages;

        }
    }
}